package com.zmn.oms.admin.advice;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.http.HttpUtil;
import com.zmn.common.utils.pager.Operator;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.oms.common.dto.OmsBaseOperator;
import com.zmn.oms.model.entity.work.OrderBaseEsQuery;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;

/**
 * 类描述:
 *
 * @author liuying
 * @date 2018/12/21 3:29 PM
 */
@Aspect
@Component
public class WebAdvice {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(public * com.zmn.oms.admin.controller..*.*(..))")
    public void pointCut() {
    }

    @Before("pointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {

        // 获取登录用户信息
        LoginStaff loginStaff = StaffAdminService.currentStaff();
        if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0 && loginStaff != null) {
            Object obj = joinPoint.getArgs()[0];
            if (obj instanceof OmsBaseOperator) {
                OmsBaseOperator omsOperator = (OmsBaseOperator) obj;
                omsOperator.setOperatorPlat(loginStaff.getPlat());
                omsOperator.setOperator(loginStaff.getRealName());
                omsOperator.setOperatorId((long) loginStaff.getStaffId());
                omsOperator.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_STAFF);
                omsOperator.setDeptId(loginStaff.getDeptId());
                omsOperator.setDeptName(loginStaff.getDeptName());
                omsOperator.setOperatorMobile(loginStaff.getMobile());
                // 设置ip
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
                omsOperator.setIp(HttpUtil.getIPAddress(request));
                logger.debug("operator-data: {}", omsOperator);
            } else if (obj instanceof OrderBaseEsQuery) {
                OrderBaseEsQuery query = (OrderBaseEsQuery) obj;
                query.setDeptId(loginStaff.getDeptId());
                query.setOrgId(loginStaff.getOrgId());
                query.setStaffId(loginStaff.getStaffId());
                query.setIdentityType(loginStaff.getIdentityType());
                query.setRoleIds(loginStaff.getRoleIds());
                query.setOperator(loginStaff.getRealName());
                // 设置ip
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
                query.setIp(HttpUtil.getIPAddress(request));
            } else if (obj instanceof Operator) {
                Operator query = (Operator) obj;
                // 设置ip
                RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
                query.setOperatorIP(HttpUtil.getIPAddress(request));
                query.setOperatorId(loginStaff.getStaffId());
            }
        }
    }

}
